package com.ruoyi.demo.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.mapper.DemoDeptMapper;
import com.ruoyi.demo.domain.DemoDept;
import com.ruoyi.demo.service.IDemoDeptService;

/**
 * 测试部门Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-13
 */
@Service
public class DemoDeptServiceImpl implements IDemoDeptService 
{
    @Autowired
    private DemoDeptMapper demoDeptMapper;

    /**
     * 查询测试部门
     * 
     * @param id 测试部门主键
     * @return 测试部门
     */
    @Override
    public DemoDept selectDemoDeptById(String id)
    {
        return demoDeptMapper.selectById(id);
    }

    /**
     * 查询测试部门列表
     * 
     * @param demoDept 测试部门
     * @return 测试部门
     */
    @Override
    public List<DemoDept> selectDemoDeptList(DemoDept demoDept)
    {
        Map<String, Object> params = demoDept.getParams();
        LambdaQueryWrapper<DemoDept> query = new LambdaQueryWrapper<>();

                    query.eq(demoDept.getParentId() != null, DemoDept::getParentId, demoDept.getParentId());
                    query.like(demoDept.getDeptName() != null, DemoDept::getDeptName, demoDept.getDeptName());



        return demoDeptMapper.selectList(query);
    }

    /**
     * 新增测试部门
     * 
     * @param demoDept 测试部门
     * @return 结果
     */
    @Override
    public int insertDemoDept(DemoDept demoDept)
    {
        demoDept.setCreateTime(DateUtils.getNowDate());
        demoDept.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            String parentId = demoDept.getParentId();
            if("0".equals(parentId)){
                demoDept.setAncestors("0");
                demoDept.setParentName("");
            }else{
                LambdaQueryWrapper<DemoDept> query = new LambdaQueryWrapper<>();
                query.eq(parentId != null,DemoDept::getId,parentId);
                DemoDept parentDemoDept = demoDeptMapper.selectOne(query);
                if(parentDemoDept != null){
                    demoDept.setAncestors(parentDemoDept.getAncestors()+","+parentId);
                    demoDept.setParentName(parentDemoDept.getParentName()+","+parentDemoDept.getDeptName());
                }
            }
        return demoDeptMapper.insert(demoDept);
    }

    /**
     * 修改测试部门
     * 
     * @param demoDept 测试部门
     * @return 结果
     */
    @Override
    public int updateDemoDept(DemoDept demoDept)
    {
        demoDept.setUpdateTime(DateUtils.getNowDate());
        demoDept.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            String parentId = demoDept.getParentId();
            if("0".equals(parentId)){
                demoDept.setAncestors("0");
                demoDept.setParentName("");
            }else{
                LambdaQueryWrapper<DemoDept> query = new LambdaQueryWrapper<>();
                query.eq(parentId != null,DemoDept::getId,parentId);
                DemoDept parentDemoDept = demoDeptMapper.selectOne(query);
                if(parentDemoDept != null){
                    demoDept.setAncestors(parentDemoDept.getAncestors()+","+parentId);
                    demoDept.setParentName(parentDemoDept.getParentName()+","+parentDemoDept.getDeptName());
                }
            }
        return demoDeptMapper.updateById(demoDept);
    }

    /**
     * 批量删除测试部门
     * 
     * @param ids 需要删除的测试部门主键
     * @return 结果
     */
    @Override
    public int deleteDemoDeptByIds(String[] ids)
    {
            LambdaQueryWrapper<DemoDept> query = new LambdaQueryWrapper<>();
            query.in(ids != null && ids.length > 0,DemoDept::getParentId,ids);
            List<DemoDept> childDemoDept = demoDeptMapper.selectList(query);
            if(childDemoDept !=null && !childDemoDept.isEmpty()){
                throw new RuntimeException("存在子节点不允许删除");
            }

        return demoDeptMapper.deleteBatchIds(Arrays.asList(ids));
    }
    /**
     * 删除测试部门信息
     * 
     * @param id 测试部门主键
     * @return 结果
     */
    @Override
    public int deleteDemoDeptById(String id)
    {
        LambdaQueryWrapper<DemoDept> query = new LambdaQueryWrapper<>();
        query.eq(id != null ,DemoDept::getParentId,id);
        List<DemoDept> childDemoDept = demoDeptMapper.selectList(query);
        if(childDemoDept !=null && !childDemoDept.isEmpty()){
            throw new RuntimeException("存在子节点不允许删除");
        }


        return demoDeptMapper.deleteById(id);
    }
}
